﻿import QtQuick 2.0

Item {
    property Flickable flick: null;
    signal setRolePosition(int x,int y);
    
    Component.onCompleted: Qt.callLater(initMaterial);
    
    /*!初始化素材
     */
    function initMaterial(){
        itemRep.model = Number(grid.columns * Number(grid.height/grid.itemWd));
        //Qt.callLater(colorTimer.start);
    }
    
    MouseArea{
        anchors.fill: parent;
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            if( mouse.button === Qt.LeftButton ){
                
            }
            else if( mouse.button === Qt.RightButton ){
                setRolePosition(mouseX,mouseY);
            }
        }
    }
    
    Grid{
        id: grid;
        property int itemWd: Math.max(100/1920*flick.width,66);
        parent: flick.contentItem;
        columns: Number(width/itemWd);
        anchors.fill: parent;
        spacing: 2;
        
        Timer{
            id: colorTimer;
            property int tIndex: 0;
            interval: 400;
            repeat: true;
            onTriggered: {
                if( itemRep.count <= 0 )
                    return;
                var tIndex =  parseInt(Math.random()*100000)%itemRep.count;
                itemRep.itemAt(tIndex).change();
            }
        }
        
        Repeater{
            id: itemRep
            delegate: Rectangle{
                width: grid.itemWd;
                height: width;
                opacity: 0.6;
                
                function change(){
                    var tc = parseInt((Math.random()*10000))%255/255;
                    color = Qt.rgba(((Math.random()*10000))%255/255
                                    ,((Math.random()*10000))%255/255
                                    ,((Math.random()*10000))%255/255,1);
                }
                
                Text {
                    text: index;
                    anchors.centerIn: parent;
                    color: "gray"
                }
            }
        }
    }
}
